<?php
class sample_master_model extends MY_Model {
	var $model = 'sample_master';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT b.KODE_BARANG,b.NAMA_BARANG,b.NAMA_MEREK,b.SPESIFIKASI,a.nama_kondisi kondisi,b.TIPE,b.HARGA,b.JUMLAH,b.KETERANGAN
									FROM ADMIN_PRIMKOKAS.MIS_BARANG b
									LEFT JOIN ADMIN_PRIMKOKAS.MIS_KONDISI a ON a.KODE_KONDISI=b.KODE_KONDISI
					WHERE	(LOWER(KODE_BARANG) LIKE LOWER('%".post('keyword')."%')) AND (LOWER(NAMA_BARANG) LIKE LOWER('%".post('keyword')."%'))
					ORDER BY ".(post('sort')=='' ? 'b.KODE_BARANG' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('sample_master', $arr_data);
		
		/*$this->AuditTrail(	'sample_master',// Table
							'Create',		// Action
							''				// Primary Key
							);*/
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('admin_primkokas.mis_barang', $arr_data, $arr_rule);
		
		/*$this->AuditTrail(	'sample_master',		// Table
							'Update',				// Action
							json_encode($arr_rule)	// Primary Key
							);*/
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('sample_master', array('ref_key' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_pkmaster)
	{
		$result	= $this->db->query("SELECT b.KODE_BARANG,b.NAMA_BARANG,b.NAMA_MEREK,b.SPESIFIKASI,a.nama_kondisi kondisi,b.TIPE,b.HARGA,b.JUMLAH,b.KETERANGAN
									FROM ADMIN_PRIMKOKAS.MIS_BARANG b
									LEFT JOIN ADMIN_PRIMKOKAS.MIS_KONDISI a ON a.KODE_KONDISI=b.KODE_KONDISI
									WHERE	b.KODE_BARANG = '".$as_pkmaster."'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('sample_detail2', $arr_rule);
			$this->db->delete('sample_detail1', $arr_rule);
			$this->db->delete('sample_master', $arr_rule);
			
			/*$this->AuditTrail(	'sample_master',		// Table
								'Delete',				// Action
								json_encode($arr_rule)	// Primary Key
								);*/
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>